package com.bailiban.aigou.admin.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.zip.InflaterInputStream;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.bailiban.aigou.admin.common.BaseAction;
import com.bailiban.aigou.admin.utils.ResponseBody;
import com.bailiban.aigou.admin.utils.SystemConfig;

@Controller
@Scope("prototype")
@ParentPackage("json-default")
@Namespace("/common")
public class CommonResourceAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Autowired
	private SystemConfig systemConfig;

	private ResponseBody responseBody;

	public ResponseBody getResponseBody() {
		return responseBody;
	}

	public void setResponseBody(ResponseBody responseBody) {
		this.responseBody = responseBody;
	}

	private File file;
	// 封装上传文件类型的属性
	private String contentType;
	// 封装上传文件名的属性
	private String fileName;

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileContentType() {
		return contentType;
	}

	public void setFileContentType(String contentType) {
		this.contentType = contentType;
	}

	public String getFileFileName() {
		return fileName;
	}

	public void setFileFileName(String fileName) {
		this.fileName = fileName;
	}

	@Action(value = "upload", results = { @Result(name = SUCCESS, type = "json", params = { "root", "responseBody" }) })
	public String upload() {
		try {
			String suffix = fileName.substring(fileName.lastIndexOf("."), fileName.length());
			String newFile = systemConfig.getFileUploadPath() + UUID.randomUUID().toString() + suffix;
			File destFile = new File(newFile);
			FileUtils.copyFile(file, destFile);

			Map<String, String> map = new HashMap<String, String>();
			map.put("fileName", destFile.getName()); // 存储文件名
			map.put("fileRealName", fileName); // 上传文件名
			responseBody = new ResponseBody();
			responseBody.setData(map);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	private InputStream attachstream;// 文件输入流
	private String attachname;// 文件名

	public InputStream getAttachstream() {
		return attachstream;
	}

	public void setAttachstream(InputStream attachstream) {
		this.attachstream = attachstream;
	}

	public String getAttachname() {
		return attachname;
	}

	public void setAttachname(String attachname) {
		this.attachname = attachname;
	}

	@Action(value = "getImage", results = { @Result(name = SUCCESS, type = "stream", params = { "contentType",
			"image/jpeg", "inputName", "attachstream", "contentDisposition", "attachment;filename=\"${attachname}\"",
			"bufferSize", "4096" }) })
	public String getImage()  {
		
		try {
			HttpServletRequest request = ServletActionContext.getRequest();
			String image = request.getParameter("image");
			File file = new File(systemConfig.getFileUploadPath() + image);
			attachstream = new FileInputStream(file);
			attachname = image;
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return SUCCESS;

	}
}
